<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="原文：How to process a CSV file five times faster in NodeJs with Rust and Napi rs 因为原文的文件目录和文件名比较随便，所以这里会修改一下。而且修改成适合win系统环境的代码，在ubuntu下也试过没任何问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍">
<meta property="og:url" content="https://wyatex.online/%E7%BF%BB%E8%AF%91/%E5%A6%82%E4%BD%95%E7%94%A8Rust%E5%92%8CNapi-rs%E5%9C%A8NodeJS%E4%B8%AD%E5%A4%84%E7%90%86CSV%E6%96%87%E4%BB%B6%E5%BF%AB%E4%BA%94%E5%80%8D/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="原文：How to process a CSV file five times faster in NodeJs with Rust and Napi rs 因为原文的文件目录和文件名比较随便，所以这里会修改一下。而且修改成适合win系统环境的代码，在ubuntu下也试过没任何问题。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-30T11:46:36.000Z">
<meta property="article:modified_time" content="2023-06-02T01:09:27.824Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="rust">
<meta property="article:tag" content="node">
<meta property="article:tag" content="napi-rs">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/%E7%BF%BB%E8%AF%91/%E5%A6%82%E4%BD%95%E7%94%A8Rust%E5%92%8CNapi-rs%E5%9C%A8NodeJS%E4%B8%AD%E5%A4%84%E7%90%86CSV%E6%96%87%E4%BB%B6%E5%BF%AB%E4%BA%94%E5%80%8D/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/%E7%BF%BB%E8%AF%91/%E5%A6%82%E4%BD%95%E7%94%A8Rust%E5%92%8CNapi-rs%E5%9C%A8NodeJS%E4%B8%AD%E5%A4%84%E7%90%86CSV%E6%96%87%E4%BB%B6%E5%BF%AB%E4%BA%94%E5%80%8D/","path":"翻译/如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍/","title":"如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍 | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Intro"><span class="nav-number">1.</span> <span class="nav-text">Intro</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CSV-file"><span class="nav-number">2.</span> <span class="nav-text">CSV file</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Nodejs-processing"><span class="nav-number">3.</span> <span class="nav-text">Nodejs processing</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Rust-processing"><span class="nav-number">4.</span> <span class="nav-text">Rust processing</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Napi-rs"><span class="nav-number">5.</span> <span class="nav-text">Napi rs</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/%E7%BF%BB%E8%AF%91/%E5%A6%82%E4%BD%95%E7%94%A8Rust%E5%92%8CNapi-rs%E5%9C%A8NodeJS%E4%B8%AD%E5%A4%84%E7%90%86CSV%E6%96%87%E4%BB%B6%E5%BF%AB%E4%BA%94%E5%80%8D/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍 | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-04-30 19:46:36" itemprop="dateCreated datePublished" datetime="2023-04-30T19:46:36+08:00">2023-04-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-06-02 09:09:27" itemprop="dateModified" datetime="2023-06-02T09:09:27+08:00">2023-06-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%BF%BB%E8%AF%91/" itemprop="url" rel="index"><span itemprop="name">翻译</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>原文：<a target="_blank" rel="noopener" href="https://www.alxolr.com/articles/how-to-process-a-csv-file-five-times-faster-in-node-js-with-rust-and-napi-rs">How to process a CSV file five times faster in NodeJs with Rust and Napi rs</a></p>
<p>因为原文的文件目录和文件名比较随便，所以这里会修改一下。而且修改成适合win系统环境的代码，在ubuntu下也试过没任何问题。</p>
<span id="more"></span>

<h2 id="Intro"><a href="#Intro" class="headerlink" title="Intro"></a>Intro</h2><p>本教程将教我们如何使用Rust和Napi rs在NodeJS中处理CSV文件。我们将使用Rust编程语言来加速CSV文件的处理，并使用Napi rs库创建本地nodejs扩展。</p>
<h2 id="CSV-file"><a href="#CSV-file" class="headerlink" title="CSV file"></a>CSV file</h2><p>在本教程中，我使用了以下<a target="_blank" rel="noopener" href="https://www3.stats.govt.nz/2018census/Age-sex-by-ethnic-group-grouped-total-responses-census-usually-resident-population-counts-2006-2013-2018-Censuses-RC-TA-SA2-DHB.zip">CSV文件</a>。</p>
<p>2006年、2013年和2018年人口普查(RC、TA、SA2、DHB)，按种族划分的年龄和性别(分组总答复)，通常是常住人口统计，CSV压缩文件，103mb)</p>
<p>解压缩后，您将得到几个文件，最大的一个约为900Mb</p>
<h2 id="Nodejs-processing"><a href="#Nodejs-processing" class="headerlink" title="Nodejs processing"></a>Nodejs processing</h2><p>我没有使用特殊的库来处理文件，以避免将性能问题归咎于库。</p>
<p>所以对于nodejs，我使用了readline模块，这是nodejs核心的一部分。</p>
<blockquote>
<p>这里先把data.csv复制到js文件同一个目录下，或者下面的路径改成解压出来的文件的目录也行</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> readline = <span class="built_in">require</span>(<span class="string">&quot;node:readline&quot;</span>)</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;node:fs&#x27;</span>)</span><br><span class="line"><span class="comment">// we will sum the last column of the CSV file</span></span><br><span class="line"><span class="keyword">let</span> sum = <span class="number">0</span></span><br><span class="line"><span class="keyword">let</span> isHeader = <span class="literal">true</span></span><br><span class="line"><span class="keyword">const</span> start = <span class="title class_">Date</span>.<span class="title function_">now</span>()</span><br><span class="line"><span class="keyword">const</span> file = fs.<span class="title function_">createReadStream</span>(<span class="string">&#x27;./data.csv&#x27;</span>) <span class="comment">// 把压缩包解压出来最大的文件改名成data.csv，方便代码编写</span></span><br><span class="line"><span class="keyword">const</span> lineReader = readline.<span class="title function_">createInterface</span>(&#123;</span><br><span class="line">    <span class="attr">input</span>: file,</span><br><span class="line">&#125;)</span><br><span class="line">lineReader</span><br><span class="line">    .<span class="title function_">on</span>(<span class="string">&quot;line&quot;</span>, <span class="function">(<span class="params">line</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// we need to skip the first line which is the header</span></span><br><span class="line">        <span class="keyword">if</span> (isHeader) &#123;</span><br><span class="line">            isHeader = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// ource csv has a comma as delimiter</span></span><br><span class="line">        <span class="keyword">const</span> fields = line.<span class="title function_">trimEnd</span>().<span class="title function_">split</span>(<span class="string">&quot;,&quot;</span>)</span><br><span class="line">        <span class="comment">// we get the last column and parse the value to integer</span></span><br><span class="line">        <span class="keyword">const</span> last = <span class="built_in">parseInt</span>(fields[fields.<span class="property">length</span> - <span class="number">1</span>])</span><br><span class="line"></span><br><span class="line">        <span class="comment">// there are acouple of lines with broken values we should ignore those</span></span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">isNaN</span>(last)) &#123;</span><br><span class="line">            sum += last</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    .<span class="title function_">on</span>(<span class="string">&quot;close&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`总共<span class="subst">$&#123;sum&#125;</span>行`</span>)</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`用时<span class="subst">$&#123;(<span class="built_in">Date</span>.now() - start) / <span class="number">1000</span>&#125;</span>s`</span>,)</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>

<p>运行一下看看时间:</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; node test1.js</span><br><span class="line">总共3345553228行</span><br><span class="line">用时 11.334s</span><br></pre></td></tr></table></figure>

<p>脚本在10秒内处理了文件。平均吞吐量是75MiB&#x2F;s左右。</p>
<h2 id="Rust-processing"><a href="#Rust-processing" class="headerlink" title="Rust processing"></a>Rust processing</h2><p>我使用以下代码进行Rust处理，它具有与Nodejs版本相同的逻辑。</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> std::io::&#123;BufRead, BufReader&#125;;</span><br><span class="line"><span class="keyword">use</span> std::fs::File;</span><br><span class="line"><span class="keyword">use</span> std::time::&#123;SystemTime&#125;;</span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">start</span> = SystemTime::<span class="title function_ invoke__">now</span>();</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">sum</span> = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">file</span> = File::<span class="title function_ invoke__">open</span>(<span class="string">&quot;./data.csv&quot;</span>).<span class="title function_ invoke__">expect</span>(<span class="string">&quot;Unable to open file&quot;</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">reader</span> = BufReader::<span class="title function_ invoke__">new</span>(file);</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">is_header</span> = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span> <span class="variable">line</span> <span class="keyword">in</span> reader.<span class="title function_ invoke__">lines</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">line</span> = line.<span class="title function_ invoke__">expect</span>(<span class="string">&quot;Unable to read line&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span> is_header &#123;</span><br><span class="line">            is_header = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// we get the last column and parse the value to integer</span></span><br><span class="line">        <span class="keyword">let</span> <span class="variable">res</span> = line</span><br><span class="line">            .<span class="title function_ invoke__">trim_end</span>()</span><br><span class="line">            .<span class="title function_ invoke__">split</span>(<span class="string">&quot;,&quot;</span>)</span><br><span class="line">            .<span class="title function_ invoke__">last</span>()</span><br><span class="line">            .<span class="title function_ invoke__">unwrap</span>()</span><br><span class="line">            .parse::&lt;<span class="type">f32</span>&gt;() <span class="comment">// some values are as floats but we still parse everything to int</span></span><br><span class="line">            .<span class="title function_ invoke__">unwrap_or</span>(<span class="number">0.0</span>) <span class="keyword">as</span> <span class="type">i64</span>;</span><br><span class="line"></span><br><span class="line">        sum += res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;总共&#123;&#125;行&quot;</span>, sum);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;用时&#123;&#125;s&quot;</span>, start.<span class="title function_ invoke__">elapsed</span>().<span class="title function_ invoke__">unwrap</span>().<span class="title function_ invoke__">as_millis</span>() <span class="keyword">as</span> <span class="type">f64</span> / <span class="number">1000.0</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行如下命令后:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cargo run --release</span><br></pre></td></tr></table></figure>

<blockquote>
<p>关键点是用 <code>--release</code> 标志运行；否则，性能会差很多。</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"> Compiling nodejs_vs_rust_stream v0.1.0 (/home/alxolr/Work/rust_land/nodejs_vs_rust_stream)</span><br><span class="line">    Finished release [optimized] target(s) in 0.17s</span><br><span class="line">     Running `target/release/nodejs_vs_rust_stream`</span><br><span class="line"></span><br><span class="line">总共3345553228行</span><br><span class="line">用时2.542s</span><br></pre></td></tr></table></figure>

<p>我们可以注意到Rust版本的速度比Nodejs版本快五倍。吞吐量为326MiB&#x2F;s。</p>
<p>现在一个合乎逻辑的问题出现了:<strong>如果我们已经有了一个巨大的Nodejs代码库，该怎么办?我们不能就这么搬去Rust</strong></p>
<blockquote>
<p>有一种方法可以在Nodejs中使用Rust，那就是使用Napi rs。</p>
</blockquote>
<h2 id="Napi-rs"><a href="#Napi-rs" class="headerlink" title="Napi rs"></a>Napi rs</h2><p>Napi-rs是一个允许你在Rust中创建Nodejs模块的库。它是Napi C库的包装器。</p>
<p>NAPI代码被编译到Nodejs可以加载的动态库中。因此，您可以使用NAPI在C&#x2F;C++或Rust中创建NodeJS模块。</p>
<p>为了生成新的Napi模块，您需要安装Napi rs cli工具:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i -g @napi-rs/cli</span><br></pre></td></tr></table></figure>

<blockquote>
<p>@napi-rs&#x2F;cli用的是yarn，如果没装yarn记得把yarn也安装一下</p>
</blockquote>
<p>然后，您可以使用以下命令创建一个新模块：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">napi new async_csv_reader</span><br></pre></td></tr></table></figure>

<p>然后提供的选项全部选默认即可</p>
<p>生成项目Rust代码后，将下面代码写在src&#x2F;lib.rs文件中。</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#![deny(clippy::all)]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> std::&#123;</span><br><span class="line">  fs::File,</span><br><span class="line">  io::&#123;<span class="keyword">self</span>, BufRead&#125;,</span><br><span class="line">  path::Path,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> napi::&#123;bindgen_prelude::AsyncTask, JsNumber, Task&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[macro_use]</span></span><br><span class="line"><span class="keyword">extern</span> <span class="keyword">crate</span> napi_derive;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// we want our function to be a promise to be executed asynchronously to not block the event loop in nodejs</span></span><br><span class="line"><span class="comment">// and for that we need to create this weird structs AsyncReadCsv and impl the Task trait for it.</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#[napi]</span></span><br><span class="line"><span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">read_csv_async</span>(path: <span class="type">String</span>) <span class="punctuation">-&gt;</span> AsyncTask&lt;AsyncReadCsv&gt; &#123;</span><br><span class="line">  AsyncTask::<span class="title function_ invoke__">new</span>(AsyncReadCsv &#123; path &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">pub</span> <span class="keyword">struct</span> <span class="title class_">AsyncReadCsv</span> &#123;</span><br><span class="line">  path: <span class="type">String</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Task</span> <span class="keyword">for</span> <span class="title class_">AsyncReadCsv</span> &#123;</span><br><span class="line">  <span class="keyword">type</span> <span class="title class_">Output</span> = <span class="type">i64</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">type</span> <span class="title class_">JsValue</span> = JsNumber;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">fn</span> <span class="title function_">compute</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>) <span class="punctuation">-&gt;</span> napi::<span class="type">Result</span>&lt;<span class="keyword">Self</span>::Output&gt; &#123;</span><br><span class="line">    <span class="title function_ invoke__">Ok</span>(<span class="title function_ invoke__">read_csv</span>(<span class="keyword">self</span>.path.<span class="title function_ invoke__">clone</span>()))</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">fn</span> <span class="title function_">resolve</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, env: napi::Env, output: <span class="keyword">Self</span>::Output) <span class="punctuation">-&gt;</span> napi::<span class="type">Result</span>&lt;<span class="keyword">Self</span>::JsValue&gt; &#123;</span><br><span class="line">    env.<span class="title function_ invoke__">create_int64</span>(output)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// this is the main function that receive the path to the csv file </span></span><br><span class="line"><span class="comment">// and start processing the data line by line</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">read_csv</span>(path: <span class="type">String</span>) <span class="punctuation">-&gt;</span> <span class="type">i64</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> <span class="variable">lines</span> = <span class="title function_ invoke__">read_lines</span>(Path::<span class="title function_ invoke__">new</span>(&amp;path)).<span class="title function_ invoke__">unwrap</span>();</span><br><span class="line"></span><br><span class="line">  <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">sum</span> = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> <span class="variable">line</span> <span class="keyword">in</span> lines &#123;</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">let</span> <span class="variable">Ok</span>(ip) = line &#123;</span><br><span class="line">      <span class="keyword">let</span> <span class="variable">res</span> = ip</span><br><span class="line">        .<span class="title function_ invoke__">trim_end</span>()</span><br><span class="line">        .<span class="title function_ invoke__">split</span>(<span class="string">&quot;,&quot;</span>)</span><br><span class="line">        .<span class="title function_ invoke__">last</span>()</span><br><span class="line">        .<span class="title function_ invoke__">unwrap</span>()</span><br><span class="line">        .parse::&lt;<span class="type">f32</span>&gt;()</span><br><span class="line">        .<span class="title function_ invoke__">unwrap_or</span>(<span class="number">0.0</span>) <span class="keyword">as</span> <span class="type">i64</span>;</span><br><span class="line"></span><br><span class="line">      sum += res;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  sum</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// useful function to read the lines from a file</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">read_lines</span>&lt;P&gt;(filename: P) <span class="punctuation">-&gt;</span> io::<span class="type">Result</span>&lt;io::Lines&lt;io::BufReader&lt;File&gt;&gt;&gt;</span><br><span class="line"><span class="keyword">where</span></span><br><span class="line">  P: <span class="built_in">AsRef</span>&lt;Path&gt;,</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">let</span> <span class="variable">file</span> = File::<span class="title function_ invoke__">open</span>(filename)?;</span><br><span class="line">  <span class="title function_ invoke__">Ok</span>(io::BufReader::<span class="title function_ invoke__">new</span>(file).<span class="title function_ invoke__">lines</span>())</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在你的项目中运行npm run build后，你会得到一个index.js、index.d.ts和async_csv_reader.win32-x64-msvc.node文件，你可以从nodejs中调用它们。</p>
<blockquote>
<p>我这里使用的是rust-gnu工具链进行编译，所以得到的node文件是win32-x64-gnu.node，需要去到index.js将里面的win32-x64-msvc全部改成win32-x64-gnu</p>
</blockquote>
<p>在根目录新建个test.js，并且把csv文件复制过来（不复制，直接改下面路径也行）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; readCsvAsync &#125; = <span class="built_in">require</span>(<span class="string">&#x27;./index&#x27;</span>)</span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">test</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> start = <span class="title class_">Date</span>.<span class="title function_">now</span>()</span><br><span class="line">    <span class="keyword">const</span> sum = <span class="keyword">await</span> <span class="title function_">readCsvAsync</span>(<span class="string">&#x27;./data.csv&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;总共&quot;</span>, sum)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;用时&quot;</span>, (<span class="title class_">Date</span>.<span class="title function_">now</span>() - start) / <span class="number">1000</span> + <span class="string">&#x27;s&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">test</span>()</span><br></pre></td></tr></table></figure>

<p>然后运行：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"> node test</span><br><span class="line">总共 3345553228</span><br><span class="line">用时 2.32s</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>对于你的nodejs代码的CPU密集部分，你需要处理大量的数据，最好使用Rust，你可以创建一个本地扩展，并从nodejs调用它。</p>
<p>这种Rust和nodejs的结合非常引人注目，并且充分利用了两者的优点。</p>
<p>我希望这篇文章对你有所帮助。如果你喜欢它，请与你的朋友分享并留下评论;我很乐意回答所有的问题。</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/%E7%BF%BB%E8%AF%91/%E5%A6%82%E4%BD%95%E7%94%A8Rust%E5%92%8CNapi-rs%E5%9C%A8NodeJS%E4%B8%AD%E5%A4%84%E7%90%86CSV%E6%96%87%E4%BB%B6%E5%BF%AB%E4%BA%94%E5%80%8D/" title="如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍">https://wyatex.online/翻译/如何用Rust和Napi-rs在NodeJS中处理CSV文件快五倍/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/rust/" rel="tag"># rust</a>
              <a href="/tags/node/" rel="tag"># node</a>
              <a href="/tags/napi-rs/" rel="tag"># napi-rs</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/TypeScript/ts%E9%BB%91%E9%AD%94%E6%B3%95%E6%95%99%E5%AD%A6%EF%BC%881%EF%BC%89%EF%BC%9A%E5%89%8D%E7%BD%AE%E7%9F%A5%E8%AF%86/" rel="prev" title="TypeScript黑魔法教学（1）：前置知识">
                  <i class="fa fa-angle-left"></i> TypeScript黑魔法教学（1）：前置知识
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/SpringBoot%E7%AC%94%E8%AE%B0/" rel="next" title="SpringBoot笔记">
                  SpringBoot笔记 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
